// pages/addnotifyperson/addnotifyperson.js
const app = getApp()
var utils = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    result: [],
    isschool: 0,
    isallteacher: 0,
    classList: [],
    teacherList: [],
    selectindex: -1, //选中的班级id
    dongtaiTOschool: 0, //全校
    dongtaiTOclass: 0, //班级
    dongtaiTOteacherall: 0, //全体教师
    dongtaiTOkidall: 0, //全体学生
    dongtaiTOkid: 0, //指定孩子
    dongtaiTOteacher: 0, //指定教师
    selectTeacherDis: [], //选择的教师列表，打开教师列表显示选中
    selectteacherlist: [], //教师列表 
    selectclassList: [], //班级列表
    selecttkidlist: [], //孩子列表
    classKidlist: [], //班级和孩子对应表
    duixiang: ""
  },

  onChange(event) {

    this.setData({
      dongtaiTOschool: 0, //全校
      dongtaiTOclass: 0, //班级
      dongtaiTOteacherall: 0, //全体教师
      dongtaiTOkidall: 0, //全体学生
      dongtaiTOkid: 0, //指定孩子
      dongtaiTOteacher: 0, //指定教师
      selectteacherlist: [],
      selectclassList: [], //班级列表
      selectTeacherDis: []
    })

    var classList = [] //重置班级列表
    console.log(event)
    var selectList = event.detail
    for (var i = 0; i < selectList.length; i++) {
      if (selectList[i] == 'dongtaiTOteacherall') {
        var teacherList = []
        //把所有的教师都添加到列表中用于打开教师列表显示选中
        for (var j = 0; j < this.data.teacherList.length; j++) {
          teacherList.push(this.data.teacherList[j].UserID + '')
        }
        this.setData({
          dongtaiTOteacherall: 1, //表明选择了所有老师
          selectTeacherDis: teacherList //用于打开教师列表显示选中
        })

      } else {

        classList.push(selectList[i])
        var a = -1,
          b = -1

        //查看被选中的班级，以及班级孩子列表是否存在该班级
        for (var j = 0; j < this.data.classList.length; j++) {
          if (selectList[i] == this.data.classList[j].ClassID) {
            a = j
            if (this.data.classKidlist != null) {
              for (var r = 0; r < this.data.classKidlist.length; r++) {
                if (selectList[i] == this.data.classKidlist[r].classid) {
                  b = r
                }
              }
            }

          }
        }

        var classKidlist1 = this.data.classKidlist
        //如果班级孩子列表中并不存在此班级，则增加这个班级和班级中的孩子
        if (a != -1 && b == -1) {
          var classid = this.data.classList[a].ClassID
          var kidList = this.data.classList[a].kidList
          var kidlist1 = []
          for (var j = 0; j < kidList.length; j++) {
            kidlist1.push(kidList[j].kidID + "")
          }
          classKidlist1.push({
            "classid": classid,
            "kidlist": kidlist1
          })
          this.setData({
            classKidlist: classKidlist1
          })
        } else {

          //如果班级孩子列表中存在此班级，则更新这个班级的孩子列表为所有人    
          var classid = this.data.classList[a].ClassID
          var kidList = this.data.classList[a].kidList
          //移除班级孩子列表中此班级
          classKidlist1.splice(b)
          var kidlist1 = []
          //重新增加本班级孩子列表
          for (var j = 0; j < kidList.length; j++) {
            kidlist1.push(kidList[j].kidID + "")
          }

          classKidlist1.push({
            "classid": classid,
            "kidlist": kidlist1
          })

          this.setData({
            classKidlist: classKidlist1
          })
        }

        this.setData({
          dongtaiTOclass: 1, //表明选择了班级
          selectclassList: classList
        })
      }
    }

    var classKidlist1 = this.data.classKidlist

    for (var j = 0; j < classKidlist1.length; j++) {

      var a = -1;
      for (var r = 0; r < selectList.length; r++) {
        if (classKidlist1[j].classid == selectList[r]) {
          a = r;
        }
      }

      if (a == -1) {
        classKidlist1.splice(j)
      }

    }

    this.setData({
      classKidlist: classKidlist1
    })


    //设置是否全校
    if (this.data.dongtaiTOteacherall == 1 && this.data.selectclassList != null && this.data.classList != null && this.data.selectclassList.length == this.data.classList.lenghth) {
      this.setData({
        dongtaiTOschool: 1,
        dongtaiTOteacherall: 0,
        dongtaiTOclass: 0,
        dongtaiTOkidall: 0,
        dongtaiTOkid: 0, //指定孩子
        dongtaiTOteacher: 0, //指定教师
        selectteacherlist: null, //教师列表 
        selectclassList: null, //班级列表
        selecttkidlist: null //孩子列表
      })
    } else {
      this.setData({
        dongtaiTOschool: 0
      })
    }


    this.setData({
      result: event.detail,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var dongtaiTOschool = options.dongtaiTOschool
    var dongtaiTOclass = options.dongtaiTOclass
    var dongtaiTOteacherall = options.dongtaiTOteacherall
    var dongtaiTOkidall = options.dongtaiTOkidall
    var dongtaiTOkid = options.dongtaiTOkid
    var dongtaiTOteacher = options.dongtaiTOteacher
    var teacherlist = JSON.parse(options.teacherlistStr)
    var classlist = JSON.parse(options.classListStr)
    var kidlist = JSON.parse(options.kidlistStr)
    var selectTeacherDis =JSON.parse(options.selectTeacherDisStr)
    var classKidlist =JSON.parse(options.classKidlistStr)
    var personResult=JSON.parse(options.personResultStr)

    this.setData({
      dongtaiTOschool: dongtaiTOschool,
      dongtaiTOclass: dongtaiTOclass,
      dongtaiTOteacherall: dongtaiTOteacherall,
      dongtaiTOkidall: dongtaiTOkidall,
      dongtaiTOkid: dongtaiTOkid,
      dongtaiTOteacher: dongtaiTOteacher,
      selectteacherlist: teacherlist,
      selectclassList: classlist,
      selectTeacherDis: selectTeacherDis, //选择的教师列表，打开教师列表显示选中
      selecttkidlist: kidlist, //孩子列表
      classKidlist: classKidlist, //班级和孩子对应表
      result:personResult
    })





    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // var pages = getCurrentPages();
    // var currPage = pages[pages.length - 1]; //当前页面
    // let json = currPage.data.selectteacherlist;
    // console.log(json) //为传过来的值

    var flag = -1;
    var flagclass = -1
    if(this.data.result.length>0){
      for (var i = 0; i < this.data.result.length; i++) {
        if (this.data.result[i] == 'dongtaiTOteacherall') {
          flag = i;
        } else {
  
          if (this.data.result[i] == this.data.classKidlist[this.data.selectindex].classid) {
            flagclass = i
          }
  
        }
      }
    }

   

    if (this.data.dongtaiTOteacherall == 1) {
      if (flag == -1) {
        var result = this.data.result
        result.push("dongtaiTOteacherall")
        this.setData({
          result: result
        })
      }

    } else {
      if (flag != -1) {
        var result = this.data.result
        result.splice(flag, 1)
        this.setData({
          result: result
        })
      }
    }


    var flagkid = -1;
    for (var i = 0; i < this.data.classList.length; i++) {
      if (this.data.classKidlist[this.data.selectindex].classid == this.data.classList[i].ClassID) {
        if (this.data.classKidlist[this.data.selectindex].kidlist.length == this.data.classList[i].kidList.length) {
          flagkid = i
        }
      }
    }

    if (flagclass == -1) {
      if (flagkid == 1) {
        var result = this.data.result
        result.push(this.data.classKidlist[this.data.selectindex].classid + "")
      }
    } else {
      if (flagkid == -1) {
        var result = this.data.result
        result.splice(flagclass, 1)
        this.setData({
          result: result
        })
      }
    }


    console.log(this.data.dongtaiTOteacherall) //为传过来的值
    console.log(this.data.selectTeacherDis) //为传过来的值
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // this.getData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  getData: function (e) {
    var that = this;
    var data = {
      "userid": app.globalData.userId,
      "moduleCode": "notify",
      "schoolid": app.globalData.schoolId,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "get_sendobject"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);

        if (res.data.status == 0) {
          that.setData({
            classList: res.data.data.classList,
            teacherList: res.data.data.teacherList,
            isallteacher: res.data.data.isallteacher,
            isschool: res.data.data.isschool
          })

        }
      },
      fail: function (res) {},
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },

  btnteacherclick(e) {
    var teacherListStr = JSON.stringify(this.data.teacherList)
    var selectteacherlistStr = JSON.stringify(this.data.selectTeacherDis)
    
    wx.navigateTo({
      url: '../addnotifyteacher/addnotifyteacher?teacherListStr=' + teacherListStr + "&selectteacherlistStr=" + selectteacherlistStr
    })
  },

  btnclassclick(event) {
    var index = event.currentTarget.dataset.index
    var kidList = this.data.classList[index].kidList
    var classid = this.data.classList[index].ClassID
    var kidListStr = JSON.stringify(kidList)
    var selectkidList = '';
    this.setData({
      selectindex: -1
    })
    for (var i = 0; i < this.data.classKidlist.length; i++) {
      if (classid == this.data.classKidlist[i].classid) {
        selectkidList = this.data.classKidlist[i].kidlist
        this.setData({
          selectindex: i
        })
      }
    }
    var selectkidListStr = JSON.stringify(selectkidList)
    wx.navigateTo({
      url: '../addnotifykid/addnotifykid?kidListStr=' + kidListStr + "&classid=" + classid + "&className=" + this.data.classList[index].ClassName + "&selectkidListStr=" + selectkidListStr
    })
  },

  btnclick(e) {

    var duixiangtemp = ""
    var classNameStr = ""
    var kidNamesStr = ""
    var teacherNames = ""
    //教师人员处理
    if (this.data.teacherList != null && this.data.teacherList.length > 0) {

      //存在教师，且选择的教师和实际教师数量相等，则表明发给所有教师
      if (this.data.selectTeacherDis != null && this.data.selectTeacherDis.length == this.data.teacherList.length) {
        duixiangtemp += ",@所有老师"
        this.setData({
          dongtaiTOteacherall: 1,
          dongtaiTOteacher: 0,
          selectteacherlist: []
        })
      } else {

        //存在教师，且选择的教师和实际教师数量不相等，则表明未选中所有教师，
        var teacherList = []
        if (this.data.selectTeacherDis != null && this.data.selectTeacherDis.length > 0) {
          for (var i = 0; i < this.data.selectTeacherDis.length; i++) {
            var teacherId = parseInt(this.data.selectTeacherDis[i])
            for (var j = 0; j < this.data.teacherList.length; j++) {
              if (teacherId == this.data.teacherList[j].UserID) {
                teacherNames += ",@" + this.data.teacherList[j].Name
              }
            }
            teacherList.push(teacherId)
          }
        }

        //如果选中教师的数量大于零则表明发给指定老师
        if (teacherList.length > 0) {
          this.setData({
            dongtaiTOteacherall: 0,
            dongtaiTOteacher: 1,
            selectteacherlist: teacherList
          })
          duixiangtemp += teacherNames
        } else { //如果选中教师的数量小于等于0，则表明也没有发给指定教师
          this.setData({
            dongtaiTOteacherall: 0,
            dongtaiTOteacher: 0,
            selectteacherlist: teacherList
          })
        }

      }
    } else {
      var flag = false;
      if (this.data.result != null && this.data.result.length > 0) {
        for (var i = 0; i < this.data.result.length; i++) {
          if (this.data.result[i] == 'dongtaiTOteacherall') {
            flag = true
          }
        }
      }

      //如果不存在教师，但是用户选中了教师复选框，则表明发给全部老师
      if (flag) {
        this.setData({
          dongtaiTOteacherall: 1,
          dongtaiTOteacher: 0,
          selectteacherlist: []
        })
        duixiangtemp += ",@所有老师"
      } else { //如果不存在教师，用户也未选中了教师复选框，则表明不发给老师和全部老师
        this.setData({
          dongtaiTOteacherall: 0,
          dongtaiTOteacher: 0,
          selectteacherlist: []
        })
      }
    }


    //班级、学生处理

    //如果存在班级
    if (this.data.classList != null && this.data.classList.length > 0) {
      var classList = []
      var kidList = []

      //如果选择了学生
      if (this.data.classKidlist != null && this.data.classKidlist.length > 0) {

        //遍历所有班级列表
        for (var i = 0; i < this.data.classList.length; i++) {
          var flag = -1
          for (var j = 0; j < this.data.classKidlist.length; j++) {
            if (this.data.classList[i].ClassID == this.data.classKidlist[j].classid) {
              flag = j
            }
          }


          //如果选择的孩子存在该班级，并且选择的孩子人数与班级内人数相同，则表示选择了班级内所有学生也就是选择了这个班级
          if (flag != -1) {
            if (this.data.classList[i].kidList.length == this.data.classKidlist[flag].kidlist.length) {
              classList.push(this.data.classList[i].ClassID)
              classNameStr += ",@" + this.data.classList[i].ClassName
            } else { //如果班级人数不等则表明只选择的学生并未选择这个班级          
              for (var j = 0; j < this.data.classKidlist[flag].kidlist.length; j++) {
                kidList.push(parseInt(this.data.classKidlist[flag].kidlist[j]))
                for(var r=0;r<this.data.classList[i].kidList.length;r++){
                  if(this.data.classList[i].kidList[r].kidID==parseInt(this.data.classKidlist[flag].kidlist[j])){
                    kidNamesStr+=",@"+this.data.classList[i].kidList[r].kidName
                  }
                }
              }

            }
          }
        }

        //如果班级列表大于0，则表明发给了指定班级
        if (classList.length > 0) {
          this.setData({
            dongtaiTOclass: 1,
            selectclassList: classList, //班级列表
          })
          if (classList.length == this.data.classList) {
            this.setData({
              dongtaiTOkidall: 1,
            })
            duixiangtemp += ",@所有学生"
          } else {
            duixiangtemp += classNameStr
            this.setData({
              dongtaiTOkidall: 0,
            })
          }

        } else { //如果班级列表小于等于0，则表明没有发给指定班级
          this.setData({
            dongtaiTOclass: 0,
            dongtaiTOkidall: 0,
            selectclassList: [], //班级列表
          })
        }

        //如果孩子列表大于0，则表明发给了指定孩子
        if (kidList.length > 0) {
          this.setData({
            dongtaiTOkid: 1,
            dongtaiTOkidall: 0,
            selecttkidlist: kidList, //孩子列表
          })
          duixiangtemp += kidNamesStr
        } else { //如果孩子列表小于等于0，则表明没有发给了指定孩子
          this.setData({
            dongtaiTOkid: 0,
            dongtaiTOkidall: 0,
            selecttkidlist: [], //孩子列表
          })
        }



      } else {
        this.setData({
          dongtaiTOclass: 0,
          dongtaiTOkid: 0,
          selectclassList: [], //班级列表
          selecttkidlist: [], //孩子列表
        })
      }

    } else {
      this.setData({
        dongtaiTOclass: 0,
        dongtaiTOkid: 0,
        selectclassList: [], //班级列表
        selecttkidlist: [], //孩子列表
      })
    }


    if (this.data.dongtaiTOteacherall == 1 && this.data.dongtaiTOkidall == 1) {
      this.setData({
        dongtaiTOschool: 1,
        dongtaiTOteacherall: 0,
        dongtaiTOkidall: 0,
        dongtaiTOclass: 0,
        dongtaiTOkid: 0,
        dongtaiTOteacher: 0,
        selectclassList: [], //班级列表
        selecttkidlist: [], //孩子列表
        selectteacherlist: [], //教师列表 
      })
      duixiangtemp = "@全校"
    } else {
      this.setData({
        dongtaiTOschool: 0
      })
    }


    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    prevPage.setData({
      dongtaiTOschool: this.data.dongtaiTOschool,
      dongtaiTOteacherall: this.data.dongtaiTOteacherall,
      dongtaiTOkidall: this.data.dongtaiTOkidall,
      dongtaiTOclass: this.data.dongtaiTOclass,
      dongtaiTOkid: this.data.dongtaiTOkid,
      dongtaiTOteacher: this.data.dongtaiTOteacher,
      classList: this.data.selectclassList,
      kidlist: this.data.selecttkidlist,
      teacherlist: this.data.selectteacherlist,
      duixiang: duixiangtemp.substring(1),
      selectTeacherDis: this.data.selectTeacherDis, //选择的教师列表，打开教师列表显示选中
      classKidlist: this.data.classKidlist, //班级和孩子对应表
      personResult:this.data.result
    })
    wx.navigateBack({ //返回
      delta: 1
    })


    // wx.navigateBack({
    //   url: '../addnotify/addnotify'
    // })



  },
})